package com.techsoul.module.order.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.constant.ApiConstants;
import com.techsoul.common.core.exception.BusinessException;
import com.techsoul.common.core.exception.ParamException;
import com.techsoul.common.core.exception.file.FileException;
import com.techsoul.common.core.manager.AsyncManager;
import com.techsoul.common.core.pojo.vo.CategoryVo;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.bean.BeanUtils;
import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.core.util.collection.MapUtils;
import com.techsoul.common.core.util.file.ResourceUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.core.util.lang.NumberUtils;
import com.techsoul.common.core.util.lang.StringUtils;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.basic.mapper.SupplierMapper;
import com.techsoul.module.basic.pojo.entity.Supplier;
import com.techsoul.module.goods.pojo.vo.SpuVo;
import com.techsoul.module.goods.service.ICategoryService;
import com.techsoul.module.goods.service.ISpuService;
import com.techsoul.module.marketing.pojo.vo.CouponVo;
import com.techsoul.module.marketing.service.ICouponService;
import com.techsoul.module.order.enmus.CartItemTypeEnum;
import com.techsoul.module.order.enmus.SalesOrderEventEnum;
import com.techsoul.module.order.enmus.SalesOrderStatusEnum;
import com.techsoul.module.order.mapper.PurchaseOrderItemMapper;
import com.techsoul.module.order.mapper.PurchaseOrderMapper;
import com.techsoul.module.order.mapper.SalesOrderItemMapper;
import com.techsoul.module.order.mapper.SalesOrderMapper;
import com.techsoul.module.order.pojo.entity.PurchaseOrderItem;
import com.techsoul.module.order.pojo.entity.SalesOrder;
import com.techsoul.module.order.pojo.entity.SalesOrderItem;
import com.techsoul.module.order.pojo.param.*;
import com.techsoul.module.order.pojo.vo.SalesOrderConfigVo;
import com.techsoul.module.order.pojo.entity.PurchaseOrder;
import com.techsoul.module.order.pojo.vo.SalesOrderItemVo;
import com.techsoul.module.order.pojo.vo.SalesOrderVo;
import com.techsoul.module.order.service.*;
import com.techsoul.module.order.task.SalesOrderAsyncFactory;
import com.techsoul.module.system.service.IConfigService;
import com.techsoul.module.system.service.IGeneralSeqService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.*;

/**
 * 销售订单 Service实现
 * 
 * @author liuen
 * @date 2020-12-07
 */
@Slf4j
@Service
@Transactional(readOnly = true)
public class SalesOrderServiceImpl implements ISalesOrderService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private SalesOrderMapper mapper;
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;
    @Autowired
    private PurchaseOrderItemMapper purchaseOrderItemMapper;
    @Autowired
    private SalesOrderItemMapper salesOrderItemMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IGeneralSeqService generalSeqService;

    @Autowired
    private ISalesOrderItemService salesOrderItemService;

    @Autowired
    private ISalesOrderEventService salesOrderEventService;

    @Autowired
    private ISalesOrderConfigService salesOrderConfigService;

    @Autowired
    private SupplierMapper supplierMapper;

    @Autowired
    private ISpuService spuService;

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IConfigService configService;

    @Autowired
    private ICartService cartService;

    @Autowired
    private ICouponService couponService;

    /**
     * 新增
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String insert(SalesOrderSaveParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        SalesOrder entity = new SalesOrder();
        // 完善订单信息
        this.perfectOrder(params);
        BeanUtils.copyProperties(params, entity);
        entity.setId(idWorker.nextId() + "").preInsert(user.getId());
        // 生成订单号
        entity.setSalesOrderNo(this.generalSeqService.generateSeq("oms_sales_order"));
        this.mapper.insert(entity);
        // 新增订单明细
        if (ListUtils.isNotEmpty(params.getItems())) {
            BigDecimal seq = new BigDecimal(0);
            DecimalFormat df = new DecimalFormat("000");
            for (SalesOrderItemSaveParam item : params.getItems()) {
                seq = seq.add(BigDecimal.valueOf(1));
                item.setSalesOrderId(entity.getId())
                        .setSalesOrderNo(entity.getSalesOrderNo())
                        .setSalesOrderItemNo(entity.getSalesOrderNo() + df.format(seq));
                // 完善订单明细
                this.perfectOrderItem(item);
                salesOrderItemService.insert(item);
            }
        }
        // 回写订单号到保存参数
        params.setId(entity.getId());
        params.setSalesOrderNo(entity.getSalesOrderNo());
        return entity.getId();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer delete(String id) {
        SalesOrder salesOrder = new SalesOrder();
        salesOrder.setIsValid(ApiConstants.N)
                .setId(id);
        return this.mapper.updateById(salesOrder);
    }

    /**
     * 批量删除
     * @param idList
     */
    @Transactional(readOnly = false)
    @Override
    public void batchDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            idList.forEach(id -> this.delete(id));
        }
    }

    /**
     * 修改
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer update(SalesOrderSaveParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        SalesOrder entityFromDb = this.mapper.selectById(params.getId());
        if (entityFromDb == null) {
            throw new ParamException("id=" + params.getId());
        }
        // 完善订单信息
        this.perfectOrder(params);
        BeanUtils.copyProperties(params, entityFromDb);
        entityFromDb.preUpdate(user.getId());
        return this.mapper.updateById(entityFromDb);
    }

    /**
     * 更新明细，全删全入
     * @param params
     */
    @Transactional(readOnly = false)
    public void updateItems(SalesOrderSaveParam params) {
        salesOrderItemService.deleteByOrderId(params.getId());
        if (ListUtils.isNotEmpty(params.getItems())) {
            BigDecimal seq = new BigDecimal(0);
            DecimalFormat df = new DecimalFormat("000");
            for (SalesOrderItemSaveParam item : params.getItems()) {
                seq = seq.add(BigDecimal.valueOf(1));
                item.setSalesOrderId(params.getId())
                        .setSalesOrderNo(params.getSalesOrderNo())
                        .setSalesOrderItemNo(params.getSalesOrderNo() + df.format(seq));
                // 完善订单明细
                this.perfectOrderItem(item);
                salesOrderItemService.insert(item);
            }
        }
    }

    /**
     * 预处理订单
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public List<CartItem> preProcessing(SalesOrderSaveParam params) {
        List<CartItem> cartItems = ListUtils.newArrayList();
        String orderType = params.getOrderType();
        StopWatch stopWatch = new StopWatch("订单预处理，orderType=" + orderType);
        stopWatch.start("1. 获取购物车商品");
        stopWatch.stop();
        if ("1".equals(orderType)) {
            stopWatch.start("2. 处理直接购买");
            stopWatch.stop();
        }
        if ("2".equals(orderType)) {
            stopWatch.start("2. 处理购物车购买");
            stopWatch.stop();
        }
        if ("3".equals(orderType)) {
            stopWatch.start("2. 处理秒杀订单购买");
            Integer limit = 1;
            if (params.getTotalQty() > limit) {
                throw new BusinessException("超过秒杀个数！");
            }
            stopWatch.stop();
        }
        stopWatch.start("3. 校验库存");
        stopWatch.stop();
        stopWatch.start("4. 计算运费");
        stopWatch.stop();
        stopWatch.start("5. 获取可用优惠券");
        stopWatch.stop();
        stopWatch.start("6. 计算总金额、优惠金额、应付金额");
        stopWatch.stop();
        log.info(stopWatch.prettyPrint());
        return cartItems;
    }


    /**
     * 保存订单-新增/修改
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public void save(SalesOrderSaveParam params) {
        String orderId = params.getId();
        params.setOrderStatus(SalesOrderStatusEnum.CREATED.getValue()); // 已创建
        if (StringUtils.isBlank(params.getId())) { // 新增
            orderId = this.insert(params);
            // 订单新增事件
            salesOrderEventService.insert(params, SalesOrderEventEnum.SO_001_001);
        } else { // 更新
            this.update(params);
            this.updateItems(params);
            // 订单保存事件
            salesOrderEventService.insert(params, SalesOrderEventEnum.SO_001_002);
        }
        // 删除购物车中与该订单明细一致的商品
        params.getItems().forEach(item -> this.cartService.delGoods(CartItemTypeEnum.EXTERNAL_MALL.getValue(), item.getGoodsId()));
        // 异步生成提案
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        AsyncManager.me().execute(SalesOrderAsyncFactory.generateQuote(orderId, params.getItems(), user));
    }

    /**
     * 提交订单-保存/修改
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String submit(SalesOrderSaveParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        String orderId = params.getId();
        params.setOrderStatus(SalesOrderStatusEnum.SUBMITTED.getValue()); // 已提交
        // 判断是否需要启动审核流程
        SalesOrderConfigVo salesOrderConfigVo = salesOrderConfigService.findByStoreId(user.getStoreId());
//        if (salesOrderConfigVo != null && ApiConstants.Y.equals(salesOrderConfigVo.getEnableAudit())) {
//            taskService.startProcess(ActEnum.SALES_ORDER_AUDIT.getProcDefKey(), ActEnum.SALES_ORDER_AUDIT.getBusinessTable(), orderId, params.getSalesOrderNo());
//        } else {
//            params.setOrderStatus(SalesOrderStatusEnum.AUDITED.getValue())
//                    .setSupervisorAuditRemark("自动审核通过")
//                    .setDeptManagerAuditRemark("自动审核通过")
//                    .setGeneralManagerAuditRemark("自动审核通过");
//        }
        if (StringUtils.isBlank(params.getId())) {
            // 新增订单
            orderId = this.insert(params);
            // 订单新增事件
            salesOrderEventService.insert(params, SalesOrderEventEnum.SO_001_001);
            // 订单提交事件
            salesOrderEventService.insert(params, SalesOrderEventEnum.SO_002_001);
        } else {
            // 更新订单
            this.update(params);
            this.updateItems(params);
            // 订单提交事件
            SalesOrderEventSaveParam insertEvent = new SalesOrderEventSaveParam();
            insertEvent.setSalesOrderId(orderId)
                    .setSalesOrderNo(params.getSalesOrderNo())
                    .setEventCode(SalesOrderEventEnum.SO_001_001.getEventCode())
                    .setReasonDesc(SalesOrderEventEnum.SO_001_001.getEventName())
                    .setOperatorName(user.getNickName());
            salesOrderEventService.insert(insertEvent);
        }
        // 删除购物车中与该订单明细一致的商品

        params.getItems().forEach(item -> this.cartService.delGoods(CartItemTypeEnum.EXTERNAL_MALL.getValue(), item.getId()));
        return orderId;
    }

    /**
     * 审核保存
     * @param act
     */
//    @Transactional(readOnly = false)
//    @Override
//    public void auditSave(Act act) {
//        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
//        Map<String, Object> variables = MapUtils.newHashMap();
//        // 获取订单信息
//        SalesOrderVo salesOrderVo = this.mapper.findByProcInsId(act.getProcInsId());
//        // 设置审核意见
//        act.setComment(ApiConstants.ACT_AUDIT_PASS.equals(act.getFlag()) ? "[同意] " : "[驳回] " + act.getComment());
//        // 对不同环节的业务逻辑进行操作
//        String taskDefKey = act.getTaskDefKey();
//        if ("supervisor_audit".equals(taskDefKey)) { // 主管审核信息
//            salesOrderVo.setSupervisorAuditBy(user.getId());
//            salesOrderVo.setSupervisorAuditRemark(act.getComment());
//            salesOrderVo.setSupervisorAuditBy(user.getId())
//                    .setSupervisorAuditRemark(act.getComment());
//        } else if ("dept_manager_audit".equals(taskDefKey)) { // 部门经理审核信息
//            salesOrderVo.setDeptManagerAuditBy(user.getId())
//                    .setDeptManagerAuditRemark(act.getComment());
//        } else if ("general_manager_audit".equals(taskDefKey)) { // 总经理审核信息
//            salesOrderVo.setGeneralManagerAuditBy(user.getId())
//                    .setGeneralManagerAuditRemark(act.getComment());
//            // 总经理审核过后更改订单状态-审核通过
//            if (ApiConstants.ACT_AUDIT_PASS.equals(act.getFlag())) {
//                salesOrderVo.setOrderStatus(SalesOrderStatusEnum.AUDITED.getValue());
//                // 订单提交事件
//                SalesOrderSaveParam salesOrderSaveParam = new SalesOrderSaveParam();
//                BeanUtils.copyProperties(salesOrderVo, salesOrderSaveParam);
//                salesOrderEventService.insert(salesOrderSaveParam, SalesOrderEventEnum.SO_002_001);
//            }
//        } else { // 未知环节，直接返回
//            return;
//        }
//        // 提交任务
//        variables.put("pass", ApiConstants.ACT_AUDIT_PASS.equals(act.getFlag()) ? "1" : "0");
//        taskService.complete(act.getTaskId(), act.getProcInsId(), act.getComment(), variables);
//        // 保存订单
//        SalesOrderSaveParam salesOrderSaveParam = new SalesOrderSaveParam();
//        BeanUtils.copyProperties(salesOrderVo, salesOrderSaveParam);
//        this.update(salesOrderSaveParam);
//
//    }

    /**
     * 下载提案/报价
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String downloadQuote(String id) {
        SalesOrder salesOrder = this.mapper.selectById(id);
        salesOrder.setOrderStatus(SalesOrderStatusEnum.UNCONFIRMED.getValue());
        String baseUrl = configService.findByConfigKey("project.attachment.baseUrl");
        return baseUrl + salesOrder.getQuoteUrl();
    }

    /**
     * 确认提案
     * @param id
     */
    @Transactional(readOnly = false)
    @Override
    public void confirmQuote(String id) {
        SalesOrder salesOrder = this.mapper.selectById(id);
        salesOrder.setConfirmQuoteTime(new Date())
                .setOrderStatus(SalesOrderStatusEnum.CONFIRMED.getValue());
        this.mapper.updateById(salesOrder);
        // todo 事件记录
    }

    /**
     * 签订合同
     * @param params
     */
    @Transactional(readOnly = false)
    @Override
    public void signContract(SalesOrderSaveParam params) {
//        params.setSignContractTime(new Date())
//                .setOrderStatus(SalesOrderStatusEnum.CONTRACT_SIGNED.getValue());
        this.update(params);
        // todo 事件记录
    }
    /**
     * 确认采购
     * @param params
     */
    @Transactional(readOnly = false)
    @Override
    public void confirmPurchase(SalesOrderSaveParam params) {
        List<String> ids = params.getIds();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        List<String> nums = params.getNums();
        List<String> suppliers = params.getSuppliers();
        List<String> prices = params.getPrices();
        List<String> salesOrderIds = new ArrayList<String>();
        List<String> usedSuppliers = new ArrayList<String>();
        SalesOrder salesOrder = new SalesOrder();
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        for(int i=0;i<ids.size();i++){
            SalesOrderItem item = this.salesOrderItemMapper.selectById(ids.get(i));
            if(item.getPurchaseStatus()!=null&&item.getPurchaseStatus()!=""){
                continue;
            }else{
                purchaseOrder= this.genPurchaseOrder(item,suppliers.get(i),user);
                this.genPurchaseOrderItem(item,salesOrder,purchaseOrder,nums.get(i),suppliers.get(i),prices.get(i),user);
                item.setPurchaseStatus("已采购");
                item.preUpdate(user.getId());
                this.salesOrderItemMapper.updateById(item);
            }

        }

    }

    /**
     * 确认支付
     * @param params
     */
    @Transactional(readOnly = false)
    @Override
    public void confirmPay(SalesOrderSaveParam params) {
        params.setPayTime(new Date())
                .setOrderStatus(SalesOrderStatusEnum.PAID.getValue());
        this.update(params);
        // todo 事件记录
    }

    /**
     * 发货计划
     * @param params
     */
    @Transactional(readOnly = false)
    @Override
    public void deliveryPlan(SalesOrderSaveParam params) {
        params.setOrderStatus(SalesOrderStatusEnum.DELIVERY_PLAN.getValue());
        this.update(params);
        // todo 事件记录
    }

    /**
     * 发货
     * @param params
     */
    @Transactional(readOnly = false)
    @Override
    public void delivery(SalesOrderSaveParam params) {
        params.setDeliveryTime(new Date())
                .setOrderStatus(SalesOrderStatusEnum.DELIVERED.getValue());
        this.update(params);
        // todo 事件记录
    }

    /**
     * 确认收货
     * @param params
     */
    @Transactional(readOnly = false)
    @Override
    public void confirmReceipt(SalesOrderSaveParam params) {
        params.setEndTime(new Date())
                .setOrderStatus(SalesOrderStatusEnum.COMPLETED.getValue());
        this.update(params);
        // todo 事件记录
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public SalesOrderVo findById(String id) {
        return this.mapper.findById(id);
    }

    /**
     * 根据订单号获取详情
     * @param salesOrderNo
     * @return
     */
    @Override
    public SalesOrderVo findBySalesOrderNo(String salesOrderNo) {
        return this.mapper.findBySalesOrderNo(salesOrderNo);
    }

    /**
     * 根据流程实例获取详情
     * @param procInsId
     * @return
     */
    @Override
    public SalesOrderVo findByProcInsId(String procInsId) {
        return this.mapper.findByProcInsId(procInsId);
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<SalesOrderVo> findAll(SalesOrderSearchParam params) {
        return this.mapper.findAll(params);
    }

    /**
     * 列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<SalesOrderVo> findAll(SalesOrderSearchParam params, Integer current, Integer pageSize) {
        return this.mapper.findAll(new Page<>(current, pageSize), params);
    }

    /**
     * 我的订单-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<SalesOrderVo> findMyOrders(SalesOrderSearchParam params, Integer current, Integer pageSize) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        params.setCreateBy(user.getId());
        return this.findAll(params, current, pageSize);
    }

    /**
     * 待审核任务列表
     * @return
     */
//    @Override
//    public List<Act> auditingList() {
//        TaskSearchParam params = new TaskSearchParam();
//        params.setProcDefKey(ActEnum.SALES_ORDER_AUDIT.getProcDefKey());
//        return taskService.todoList(params);
//    }

    /**
     * 已审核任务列表-分页
     * @param current
     * @param pageSize
     * @return
     */
//    @Override
//    public IPage<Act> auditedList(Integer current, Integer pageSize) {
//        TaskSearchParam params = new TaskSearchParam();
//        params.setProcDefKey(ActEnum.SALES_ORDER_AUDIT.getProcDefKey());
//        return taskService.historicList(params, current, pageSize);
//    }

    /**
     * 完善订单
     * @param params
     */
    private void perfectOrder(SalesOrderSaveParam params) {
        // 计算商品数量、订单金额、实付金额
        List<SalesOrderItemSaveParam> items = params.getItems();
//        if (ListUtils.isNotEmpty(items)) {
//            Long totalQty = 0L;
//            Double totalMoney = 0.0;
//            Double payMoney = 0.0;
//            // 计算商品数量和未优惠时的总金额
//            for (SalesOrderItemSaveParam item : items) {
//                totalQty += item.getQty();
//                totalMoney = NumberUtils.add(totalMoney, NumberUtils.mul(item.getQty(), item.getPrice()));
//            }
//            // 计算实付金额，实付金额 = 总金额 - 优惠金额 - 优惠券抵扣金额 - 运费
//            // 优惠金额
//            if (params.getPreMoney() != null) {
//                payMoney = NumberUtils.sub(totalMoney, params.getPreMoney());
//            } else {
//                payMoney = totalMoney;
//            }
//            // 优惠券抵扣金额
//            if (StringUtils.isNotBlank(params.getCouponId())) {
//                CouponVo couponVo = couponService.findById(params.getCouponId());
//                params.setCouponAmount(couponVo.getAmount());
//                payMoney = NumberUtils.sub(payMoney, couponVo.getAmount());
//            }
//            // 运费
//            if (params.getFreightFee() != null) {
//                payMoney = NumberUtils.sub(payMoney, params.getFreightFee());
//            }
//            params.setTotalQty(totalQty)
//                    .setTotalMoney(totalMoney)
//                    .setPayMoney(payMoney);
//        }
    }

    /**
     * 完善订单明细
     * @param item
     */
    private void perfectOrderItem(SalesOrderItemSaveParam item) {
        SpuVo spuVo = spuService.findById(item.getGoodsId());
        if (spuVo != null) {
            item.setSpuId(spuVo.getId())
                    .setSpuName(spuVo.getSpuName());
            CategoryVo categoryVo = categoryService.findById(spuVo.getCategoryId());
            if (categoryVo != null) {
                item.setCategoryId(categoryVo.getId())
                        .setCategoryName(categoryVo.getCategoryName());
            }
        }
    }
    @Override
    public PurchaseOrder genPurchaseOrder(SalesOrderItem item,String supplier,UserVo userVo){
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        SalesOrder salesOrder = this.mapper.selectById(item.getSalesOrderId());
        BeanUtils.copyProperties(salesOrder, purchaseOrder);
        purchaseOrder.setId(idWorker.nextId() + "").preInsert(userVo.getId());
        purchaseOrder.setPurchaseOrderNo(this.generalSeqService.generateSeq("oms_purchase_order"));
        if(supplier!=null&&supplier!=""){
            purchaseOrder.setSupplierCompanyNo(supplier);
            Supplier thissupplier =this.supplierMapper.selectById(supplier);
            purchaseOrder.setSupplierName(thissupplier.getSupplierName());
            this.purchaseOrderMapper.insert(purchaseOrder);
            return purchaseOrder;
        }else{
          throw new BusinessException("没有选择采购的供应商!");
        }

    }
    @Override
    public void genPurchaseOrderItem(SalesOrderItem item,SalesOrder salesOrderVo,PurchaseOrder purchaseOrder, String nums,String supplier,String price,UserVo userVo){
        PurchaseOrderItem purchaseOrderItem = new PurchaseOrderItem();

        BeanUtils.copyProperties(item, purchaseOrderItem);
        purchaseOrderItem.setId(idWorker.nextId() + "").preInsert(userVo.getId());
        purchaseOrderItem.setPurchaseOrderNo(purchaseOrder.getPurchaseOrderNo());
        purchaseOrderItem.setSupplierId(supplier);
        if(price!=null&&price!=""&&nums!=""&&nums!=null){
            Double tot = NumberUtils.mul(Double.valueOf(price),Double.valueOf(nums));
            purchaseOrderItem.setAccountSum(BigDecimal.valueOf(tot));

        }
        if(nums!=""&&nums!=null) {
            purchaseOrderItem.setOrderQuantity(new BigDecimal(nums));
            purchaseOrderItem.setSalesQuantity(new BigDecimal(nums));
        }


        this.purchaseOrderItemMapper.insert(purchaseOrderItem);
    }

}
